#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "telephoneBook.h"

#define BUFFER_SIZE 50

enum Choice
{
    show = 1,
    insertCont,
    deleteByName,
    deleteByPhone,
    modifyContact,
    searchByName,
    searchByPhone,
    quit
};

// 自定义的释放函数
void FreeContacts(ElementType element)
{
    ContInfo *cont = (ContInfo *)element;
    if(cont)
    {
        free(cont->name);
        free(cont->phone);
        free(cont);
    }
}

// 自定义的打印函数
void PrintContacts(ElementType element)
{
    ContInfo *cont = (ContInfo *)element;
    if(cont)
    {
        printf("Name: %s, Phone: %s\n", cont->name, cont->phone);
    }
}

// 显示菜单
void ShowMenu()
{
    printf("\n请选择操作:\n");
    printf("1.显示所有联系人\n");
    printf("2.插入联系人\n");
    printf("3.按名字删除联系人\n");
    printf("4.按电话删除联系人\n");
    printf("5.修改联系人信息\n");
    printf("6.按名字模糊查询\n");
    printf("7.按电话模糊查询\n");
    printf("8.退出\n");
}

int main()
{
    // 初始化通讯录
    TelBook *book = InitTelBook(10, FreeContacts);
    // 判空
    if(book == NULL)
    {
        return -1;
    }

    int choice;
    char name[BUFFER_SIZE];
    char phone[BUFFER_SIZE];
    char newName[BUFFER_SIZE];
    char newPhone[BUFFER_SIZE];

    while(1)
    {
        ShowMenu();
        scanf("%d", &choice);

        switch(choice)
        {
            case show:
                Show(book);
                break;
            case insertCont:
                printf("请输入名字: ");
                scanf("%s", name);
                printf("请输入电话: ");
                scanf("%s", phone);
                if (InsertCont(book, name, phone)) {
                    printf("联系人插入成功！\n");
                } else {
                    printf("联系人插入失败！\n");
                }
                break;
            case deleteByName:
                printf("请输入要删除的联系人名字: ");
                scanf("%s", name);
                if (DeleteByName(book, name)) {
                    printf("联系人删除成功！\n");
                } else {
                    printf("未找到联系人！\n");
                }
                break;
            case deleteByPhone:
                printf("请输入要删除的联系人电话: ");
                scanf("%s", phone);
                if (DeleteByPhone(book, phone)) {
                    printf("联系人删除成功！\n");
                } else {
                    printf("未找到联系人！\n");
                }
                break;
            case modifyContact:
                printf("请输入要修改的联系人旧名字: ");
                scanf("%s", name);
                printf("请输入新名字: ");
                scanf("%s", newName);
                printf("请输入新电话: ");
                scanf("%s", newPhone);
                if (ModifyContact(book, name, newName, newPhone)) {
                    printf("联系人修改成功！\n");
                } else {
                    printf("未找到联系人！\n");
                }
                break;
            case searchByName:
                printf("请输入要查询的名字子串: ");
                scanf("%s", name);
                SearchByName(book, name);
                break;
            case searchByPhone:
                 printf("请输入要查询的电话子串: ");
                scanf("%s", phone);
                SearchByPhone(book, phone);
                break;
            case quit:
                FreeTelBook(book);
                return 0;
            default:
                printf("无效的选择，请重新选择！\n");
                break;
        }
    }
    return 0;
}